import request from '../util/request';
import { message } from 'antd';

export default {
    namespace: 'book-table',
    state: {
        bookList: [],
        counter:0
    },
    effects: {
        *queryList(_, sagaEffects) {
            const { call, put } = sagaEffects;
            const endPointURI = '/dev/bookList';
            try { // 加入 try catch 捕获抛错
                const bookList = yield call(request, endPointURI);
                
                yield put({ type: 'loadData', payload: bookList });
              } catch (e) {
                message.error('数据获取失败'); // 打印错误信息
              }
        }
    },
    reducers: {
        loadData(state, { payload: bookList }) {
            state.counter=0
            bookList.forEach(element => {
                element.id=state.counter;
                state.counter+=1;
            });
            return {
                bookList:bookList,
                counter:state.counter
            }
        }
    }
};